#include "baiduvoice.h"

BaiduVoice::BaiduVoice(QObject *parent) : QObject(parent)
{
    //init param
    strText="holding";
    API_id="8063858";
    API_key="TkOjjXy2atUgSzMBR4tQ3kvO";
    API_secret_key="a7013831c2de102bc772c534e81e107b";
    API_access_token="";//进入令牌
    API_language="zh";
    API_record_path="/mnt/sdcard/Android/data/baiduvoice.amr";//录音文件的路径
    flag=1;

    manager = new QNetworkAccessManager();
    connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinish(QNetworkReply*)));
    manager->get(QNetworkRequest(QUrl( "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=TkOjjXy2atUgSzMBR4tQ3kvO&client_secret=a7013831c2de102bc772c534e81e107b")));
}

void BaiduVoice::replyFinish (QNetworkReply *reply){
    if(flag==1)
    {
        QString strJsonAccess=reply->readAll();//获得http返回的信息
        qDebug()<<strJsonAccess;
        QScriptValue jsonAccess;//容器数据类型脚本
        QScriptEngine engineAccess;
        jsonAccess = engineAccess.evaluate("value = " + strJsonAccess);
        QScriptValueIterator iteratorAccess(jsonAccess);
        while (iteratorAccess.hasNext())
        {
                iteratorAccess.next();
              if(iteratorAccess.name()=="access_token")
                  API_access_token = iteratorAccess.value().toString();//得到 API_access_token,验证是百度用户
              qDebug()<<"验证"<<API_access_token;
         }
        if(API_access_token=="")
        {
            return;
        }
        flag=0;
        reply->deleteLater();//释放
    }
    else
    {
        QString strJsonText = reply->readAll();//对文本进行解析,,这个返回的JSON包所携带的是语音文本
//        QString strText="";
        qDebug()<<"dasd"<<strJsonText;
        QScriptValue jsontext;
        QScriptEngine engineText;
        jsontext = engineText.evaluate("value = " + strJsonText );
        QScriptValueIterator iteratorText(jsontext);
        while (iteratorText.hasNext())
        {
               iteratorText.next();
              if(iteratorText.name()=="result")
              {
                  strText = iteratorText.value().toString();
                  emit strtextchange();
                  break;
              }
         }
    }

}

void BaiduVoice::getText (QString para_API_id, QString para_API_access_token, QString para_API_language, QString para_API_record_path){
    {
        QFile file(para_API_record_path);
        if( !(file.open(QIODevice::ReadOnly)))
        {
            qDebug()<<"没有文件";
            return;
        }

        QDataStream in(&file);//二进制数据
        m_buf =new char[file.size()];
        in.readRawData(m_buf,file.size());//从file中读取到char中
        file.close();

        QString  getTextUrl = "http://vop.baidu.com/server_api?lan=" + para_API_language + "&cuid=" + para_API_id + "&token=" + para_API_access_token;
        QUrl url;
        url.setUrl(getTextUrl);
        QNetworkRequest request(url);
        request.setHeader(QNetworkRequest::ContentTypeHeader, "amr;rate=8000");
        QByteArray arr = QByteArray(m_buf, file.size());
         manager->post(request,arr);
    }
}
QString BaiduVoice::return_str (){
    return strText;
}
void BaiduVoice::set_path (QString path)    {
    API_record_path=path;
}
void BaiduVoice::set_gettext ()     {
    qDebug()<<"set_gettext()";
     getText(API_id,API_access_token,API_language,API_record_path);
 }
